package com.gopay.bis.web.controller.ebank;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ipay.ebank.IpayMessage;

/**
 * 民生i支付后台银行回应
 * 
 * @IPayServerRes.java
 * @author lilili
 */
@Controller
@Scope("prototype")
public class IPayServerRes extends RefundBankResBase {

	 private static Log logger = LogFactory.getLog(IPayServerRes.class);
		
	    @RequestMapping(value = "iPayServerRes")
	    protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
	    	logger.error("--------------- IPayServerRes bankback  ---------------");
			return super.execute(request, response);
	    }
	    
	    @RequestMapping(value = "iPayRefundServerRes")
        protected void executeRefund(HttpServletRequest request,HttpServletResponse response) {
            logger.error("--------------- IPayServerRes iPayRefundServerRes  bankback---------------");
            super.executeRefund(request, response);
        }

	    
	    @Override
		protected BaseMessage buildMessage(HttpServletRequest request) {
		    IpayMessage message = new IpayMessage();
	        message.setBank(BankConstants.BANK_LOGO_IPAY);
	       message.setOidPartner(request.getParameter("oid_partner"));
	       message.setMoneyOrder(request.getParameter("money_order"));
	       message.setNoOrder(request.getParameter("no_order"));
	       message.setOidPaybill(request.getParameter("oid_paybill"));
	       message.setRespType(request.getParameter("resp_type"));
	       message.setRespCode(request.getParameter("resp_code"));
	       
	       message.setPayType(request.getParameter("pay_type"));
	       message.setSignMessage(request.getParameter("sign"));
	       message.setSettleDate(request.getParameter("settle_date"));
	       
	       String respMsg = request.getParameter("resp_msg");

	       message.setRespMsg(respMsg);
	       
	       logger.error("------ipay server res  respmsg-----------"+message.getRespMsg());
	       message.setBankOrderId(message.getNoOrder());
	        //银行业务产品00个人网银支付
	        message.setBankBusProd(BisBankBusinessProd._00);
	        return message;
		}
	    
	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		 IpayMessage message = (IpayMessage)result.getModel("res");
	        OrderInfo orderInfo = new OrderInfo();
	        if(message!=null){
				String amount = message.getMoneyOrder();
				String retCode = message.getRespCode();
	            if(retCode!=null && retCode.equals("00")){
	                orderInfo.setPayStatus("00");
	                orderInfo.setRetCode("00");
	                orderInfo.setRetMsg("交易成功");
	            } else{
	                orderInfo.setPayStatus("01");
	                orderInfo.setRetCode("01");
	                orderInfo.setRetMsg("交易失败");
	            }
				orderInfo.setOrderId(message.getOrderId());
				orderInfo.setAmt(amount);
				orderInfo.setNotifyNum("1");
	            orderInfo.setRespCode(message.getRespCode());
	            orderInfo.setRespMsg(message.getRespMsg());
	            orderInfo.setBankSignMsg(message.getSignMessage());
	        }
	        orderInfo.setBankName("ipay");
	        orderInfo.setIsNotify(true);
	        return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("oid_partner"))
                || StringUtils.isEmpty(request.getParameter("money_order"))
                || StringUtils.isEmpty(request.getParameter("no_order"))
                || StringUtils.isEmpty(request.getParameter("oid_paybill"))
                || StringUtils.isEmpty(request.getParameter("resp_type"))
                || StringUtils.isEmpty(request.getParameter("resp_code"))
                || StringUtils.isEmpty(request.getParameter("resp_msg"))
                || StringUtils.isEmpty(request.getParameter("pay_type"))
                || StringUtils.isEmpty(request.getParameter("sign"))) {
        return false;
        }
        return true;
	}

	@Override
    protected BaseMessage buildRefundMessage(HttpServletRequest request) {
        IpayMessage message = new IpayMessage();
        message.setBank(BankConstants.BANK_LOGO_IPAY);
        message.setOidPartner(request.getParameter("oid_partner"));
        message.setMoneyOrder(request.getParameter("money_order"));
        message.setNoOrder(request.getParameter("no_order"));
        message.setOidPaybill(request.getParameter("oid_paybill"));
        message.setRespType(request.getParameter("resp_type"));
        message.setRespCode(request.getParameter("resp_code"));
        message.setRespMsg(request.getParameter("resp_msg"));
        message.setPayType(request.getParameter("pay_type"));
        message.setSignMessage(request.getParameter("sign"));
        message.setSettleDate(request.getParameter("settle_date"));
        // 银行业务产品12: 退款
        message.setBankBusProd(BisBankBusinessProd._12);
        message.setBankOrderId(message.getNoOrder()); // 银行订单号
        return message;
    }

    @Override
    protected boolean checkRefundParameters(HttpServletRequest request) {
        if (StringUtils.isEmpty(request.getParameter("oid_partner"))
                || StringUtils.isEmpty(request.getParameter("money_order"))
                || StringUtils.isEmpty(request.getParameter("no_order"))
                || StringUtils.isEmpty(request.getParameter("oid_paybill"))
                || StringUtils.isEmpty(request.getParameter("resp_type"))
                || StringUtils.isEmpty(request.getParameter("resp_code"))
                || StringUtils.isEmpty(request.getParameter("resp_msg"))
                || StringUtils.isEmpty(request.getParameter("pay_type"))
                || StringUtils.isEmpty(request.getParameter("sign"))) {
        return false;
        }
        return true;
    }

}
